﻿package idv.cjcat.emitter.deflectors {
	use namespace engine;
	
	import flash.errors.*;
	
	import idv.cjcat.emitter.*;
	import idv.cjcat.emitter.geom.*;
	
	/**
	 * Abstract deflector.
	 * <p>
	 * This is an abstarct class and should not be instantiated.
	 * </p>
	 */
	public class Deflector implements IEmitterXML, IEmitterDestructible {
		
		public var active:Boolean = true;
		public var bounce:Number = 0.9;
		
		public function Deflector() {
			this.id = null;
		}
		
		/**
		 * This is an abstract method and should be overriden
		 */
		public function getMotionData4D(pd:ParticleData):MotionData4D {
			return null;
		}
		
		
		//IEmitterDestructible
		//------------------------------------------------------------------------------------------------
		
		engine var _destroyed:Boolean = false;
		public function get destroyed():Boolean { return _destroyed }
		
		//------------------------------------------------------------------------------------------------
		//end of IEmitterDestructible
		
		
		//XML
		//------------------------------------------------------------------------------------------------
		
		/** @private */
		engine var _id:String;
		public function get id():String { return _id; }
		public function set id(value:String):void {
			delete EmitterGlobal._deflectors[_id];
			if (value == null) {
				while (EmitterGlobal._deflectors["deflector" + EmitterGlobal._deflectorCounter] != undefined) {
					EmitterGlobal._deflectorCounter++;
				}
				_id = "deflector" + EmitterGlobal._deflectorCounter;
			} else {
				if (EmitterGlobal._deflectors[value] == undefined) {
					_id = value;
				} else {
					throw new IllegalOperationError("This name is already used by another deflector.");
				}
			}
			EmitterGlobal._deflectors[_id] = this;
		}
		public function toXML():XML {
			var xml:XML = <deflector/>;
			xml.@id = _id;
			xml.@type = "abstract";
			xml.@active = active.toString();
			xml.@bounce = bounce.toString();
			
			return xml;
		}
		public function parseXML(xml:XML):void {
			active = (xml.@active == "true");
			bounce = parseFloat(xml.@bounce);
		}
		
		//------------------------------------------------------------------------------------------------
		//end of XML
	}
}